package com.asset.dispoal.business.repayment.service.strategy;

import com.asset.dispoal.business.bsCase.domain.db.entity.BsCase;
import com.asset.dispoal.business.mediation.domain.db.entity.ReduceOperate;
import com.asset.dispoal.business.mediation.domain.db.entity.ReduceStagingTmp;
import com.asset.dispoal.business.mediation.domain.db.mapper.ReduceOperateMapper;
import com.asset.dispoal.business.mediation.domain.db.mapper.ReduceStagingTmpMapper;
import com.asset.dispoal.business.repayment.dto.RepaymentInfoResponse;
import com.asset.dispoal.commom.menus.AgreementCaseSateMenu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tech.mhuang.pacebox.core.util.StringUtil;

import java.math.BigDecimal;
import java.util.List;

/**
 * @description: 获取和解状态的还款信息
 * @author: yf
 * @date: 2022-09-29 09:37:43
 */
@Component("reconciliationState")
public class ReconciliationCase implements IRepaymentStrategy {
    @Autowired
    private ReduceOperateMapper reduceOperateMapper;
    @Autowired
    private ReduceStagingTmpMapper reduceStagingTmpMapper;

    @Override
    public RepaymentInfoResponse getRepaymentInfo(BsCase bsCase) {
        List<ReduceOperate> reduceOperates = reduceOperateMapper.getLogsByCaseId(bsCase.getId());
        //获取最新一次的减免预设记录 应修改用户选择的减免预设记录
        ReduceOperate reduceOperate = reduceOperates.get(0);
        if (StringUtil.isNotBlank(reduceOperate.getStagingIds())) {
            ReduceStagingTmp stagingInfoByIds = reduceStagingTmpMapper.getUnrepaidPeriod(reduceOperate.getStagingIds());
            AgreementCaseSateMenu caseSateMenu = AgreementCaseSateMenu.getByLabel(bsCase.getCaseState());
            //已还本金 利息 罚息
            BigDecimal repaymentPrincipal = stagingInfoByIds.getRepaymentPrincipalAmount() == null ? BigDecimal.ZERO : stagingInfoByIds.getRepaymentPrincipalAmount();
            BigDecimal repaymentInterest = stagingInfoByIds.getRepaymentInterestAmount() == null ? BigDecimal.ZERO : stagingInfoByIds.getRepaymentInterestAmount();
            BigDecimal repaymentPenalty = stagingInfoByIds.getRepaymentLitigationFines() == null ? BigDecimal.ZERO : stagingInfoByIds.getRepaymentLitigationFines();
            //待还本金 利息 罚息
            BigDecimal toDoReturnPrincipalAmount = stagingInfoByIds.getLawsuitPrincipalAmount().subtract(repaymentPrincipal);
            BigDecimal toDoInterestAmount = stagingInfoByIds.getLawsuitInterestAmount().subtract(repaymentInterest);
            BigDecimal toDoLitigationFines = stagingInfoByIds.getLitigationFines().subtract(repaymentPenalty);


            return RepaymentInfoResponse.builder()
                    .repaymentId(stagingInfoByIds.getId())
                    .caseId(bsCase.getId())
                    .caseState(bsCase.getCaseState())
                    .caseStateName(caseSateMenu.getValue())
                    .lawsuitPrincipalAmount(stagingInfoByIds.getLawsuitPrincipalAmount().toPlainString())
                    .lawsuitInterestAmount(stagingInfoByIds.getLawsuitInterestAmount().toPlainString())
                    .litigationFines(stagingInfoByIds.getLitigationFines().toPlainString())
                    .toDoReturnPrincipalAmount(toDoReturnPrincipalAmount.toPlainString())
                    .toDoInterestAmount(toDoInterestAmount.toPlainString())
                    .toDoLitigationFines(toDoLitigationFines.toPlainString())
                    .repaymentPrincipal(repaymentPrincipal.toPlainString())
                    .repaymentInterest(repaymentInterest.toPlainString())
                    .repaymentPenalty(repaymentPenalty.toPlainString())
                    .period(stagingInfoByIds.getPeriod())
                    .isPay(stagingInfoByIds.getIsPay())
                    .build();
        }
        return null;
    }
}
